package util

import (
	"time"
)

/**
 * Convert datetime to unix timestamp
 * Example: 2021/01/01 12:00:00
 * @param datetime string
 * @param format string, options: date, datetime
 * @return int64
 */
func DatetimeToUnix(datetime string, format string) int64 {
	loc, _ := time.LoadLocation("Asia/Shanghai")
	parsedTime, err := time.ParseInLocation("2006/01/02 15:04:05", datetime, loc)
	if err != nil {
		return 0
	}

	if format == "date" {
		datetime = parsedTime.Format("2006/01/02")
		loc, _ = time.LoadLocation("Asia/Shanghai")
		parsedTime, err = time.ParseInLocation("2006/01/02", datetime, loc)
		if err != nil {
			return 0
		}
	}
	return parsedTime.Unix()
}
